import { dateFormat } from '@/const';
import type { AllFormFields, FormSubmitFields } from '@/types';
import dayjs from 'dayjs';
import { isEmpty } from 'radash';

export const format = (date: string, format = dateFormat) => dayjs(date).format(format);

function transformBed(bed: number[]) {
	if (!Array.isArray(bed)) return 0;
	return bed[bed.length - 1] || 0;
}

function getBedSelectId(data: FormSubmitFields) {
	const { FloorID, BedID, ApartmentID, RoomID } = data;
	if (isEmpty(FloorID) || isEmpty(BedID) || isEmpty(ApartmentID) || isEmpty(RoomID)) return [];
	return [ApartmentID, FloorID, RoomID, BedID];
}

export function getBedSelectName(data: FormSubmitFields) {
	const { FloorName, RoomName, BedName, ApartmentName } = data;
	if (isEmpty(FloorName) || isEmpty(RoomName) || isEmpty(BedName) || isEmpty(ApartmentName)) return '';
	return `${ApartmentName}/${FloorName}/${RoomName}/${BedName}`;
}

export function formToSubmitParam(data: AllFormFields): FormSubmitFields {
	const form = {
		Name: data.name,
		IDCard: data.card,
		Sex: data.sex,
		Address: data.addr,
		Way: data.checkInMethod,
		MaritalStatus: data.maritalStatus,
		Birthday: format(data.birth),
		Age: data.age,
		Contact: data.emergencyName,
		Listening: data.hearing,
		Phone: data.emergencyPhone,
		Eyesight: data.vision,
		Skin: data.skinCondition,
		// Disabled: data.vision,
		Hobby: data.interest,
		SurgeryHistory: data.surgicalHistory,
		DrugAllergyHistory: data.drugAllergyHistory,
		Allergen: data.allergen,
		DietaryTaboos: data.dietaryTaboos, // 饮食忌口
		Smoking: data.smoke,
		Drink: data.drink,
		Height: data.height,
		BloodPressure: data.blooPressure,
		BloodLipids: data.bloodLipids,
		BloodSugar: data.blooPressure,
		HeartRate: data.heartRate,
		CapabilityAssessment: data.capability,
		CognitiveAssessment: data.cognitive,
		CareLevel: data.careLevel,
		// Bed: data.room,
		BedID: transformBed(data.room),
		PaymentWay: data.payMethod,
		ReimbursementRatio: data.reimbursementRatio,
		StartTime: format(data.checkInDate || ''), // checkInDate
		Avatar: data.avatar, // checkInDate
		MedicalHistory: data.pastMedicalHistory,
		Weight: data.weight,
		Packages: data.packages.map(id => Number(id))
	};
	return form as any;
};

export function detailToForm(data: FormSubmitFields): AllFormFields {
	const form = {
		name: data.Name,
		card: data.IDCard,
		sex: data.Sex,
		addr: data.Address,
		checkInMethod: data.Way,
		maritalstatus: data.MaritalStatus,
		birth: format(data.Birthday || ''),
		age: data.Age,
		emergencyName: data.Contact,
		hearing: data.Listening,
		emergencyPhone: data.Phone,
		vision: data.Eyesight,
		skinCondition: data.Skin,
		// Disabled: data.vision,
		interest: data.Hobby,
		surgicalHistory: data.SurgeryHistory,
		drugAllergyHistory: data.DrugAllergyHistory,
		allergen: data.Allergen,
		dietaryTaboos: data.DietaryTaboos, // 饮食忌口
		smoke: data.Smoking,
		drink: data.Drink,
		height: data.Height,
		bloodpressure: data.BloodPressure,
		bloodlipids: data.BloodLipids,
		blooPressure: data.BloodSugar,
		heartRate: data.HeartRate,
		capability: data.CapabilityAssessment,
		cognitive: data.CognitiveAssessment,
		careLevel: data.CareLevel,
		// room: data.Bed,
		room: getBedSelectId(data),
		roomName: getBedSelectName(data),
		payMethod: data.PaymentWay,
		reimbursementRatio: data.ReimbursementRatio,
		checkInDate: format(data.StartTime || ''), // checkInDate
		avatar: data.Avatar, // checkInDate
		pastMedicalHistory: data.MedicalHistory,
		weight: data.Weight,
		packages: data.Packages || []

	};
	return form as any;
};


